<?php
/**
 * GioCMS
 * 
 * LICENSE
 *
 * This source file is subject to the GNU GENERAL PUBLIC LICENSE Version 2 
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.gnu.org/licenses/gpl-2.0.txt
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@ninhgio.com so we can send you a copy immediately.
 * 
 * @copyright	Copyright (c) 2010-2011 GioCMS (http://cms.ninhgio.com)
 * @license		http://www.gnu.org/licenses/gpl-2.0.txt GNU GENERAL PUBLIC LICENSE Version 2
 * @author		NinhGio - ninhgio@gmail.com
 * @since		1.0
 * @version		$Id:$
 */

$this->headTitle($this->TRANSLATOR->translator('module_list_page_title') . ' - ' . $this->TRANSLATOR->translator('common_admin_title'));
?>
<div class="content">
	<div class="box header mb0">
		<div class="head"><div></div></div>
		<h2 class="g_a_breadcump">
			<a class="fl" href="<?php echo $this->url('core_dashboard_index'); ?>"><?php echo $this->TRANSLATOR->translator('dashboard_breadcump'); ?></a>
			<small class="icon arrow_right" style="display: block;"></small>
			<a class="fl" href="<?php echo $this->url('core_module_list'); ?>"><?php echo $this->TRANSLATOR->translator('module_breadcump'); ?></a>
			<small class="icon arrow_right" style="display: block;"></small>
			<a class="fl" href="javascript: void(0);"><?php echo $this->TRANSLATOR->translator('module_list_breadcump'); ?></a>
			<div class="clear"></div>
		</h2>
	</div>
</div>
<div class="content">
	<div class="desc">
		<div class="body">
			<h4 class="fl g_a_h4"><?php echo $this->TRANSLATOR->translator('module_list_guide'); ?></h4>
			<div class="fr">
				<a class="fl g_a_tip g_a_cancel_button mr5" href="javascript: history.go(-1);" title="<?php echo $this->STRING->escape($this->TRANSLATOR->translator('common_back_button', 'core')); ?>"></a>
			</div>
			<div class="g_a_line"></div>
			<!--  -->
			<table class="g_a_data_table">
				<tbody>
					<tr>
						<th class="checkbox w_1"><input type="checkbox" name="checkbox"></th>
						<th><?php echo $this->TRANSLATOR->translator('module_name'); ?></th>
						<th class="w_12"><?php echo $this->TRANSLATOR->translator('module_status'); ?></th>
						<th class="w_12"><?php echo $this->TRANSLATOR->translator('module_actions'); ?></th>
					</tr>
					<?php if (isset($this->modules) && $this->modules) : ?>
					<?php foreach ($this->modules as $index => $module) : ?>
					<tr class="g_a_row" id="gCoreModuleRow_<?php echo $module; ?>">
						<td class="checkbox w_1"><input type="checkbox" name="checkbox"></td>
						<td>
							<div class="content"><strong><?php echo $this->TRANSLATOR->translator('about_' . $module . '_title', $module); ?></strong></div>
						</td>
						<td class="w_12" id="gCoreModuleRowStatus_<?php echo $module; ?>">
							<?php if (($moduleInstalled = Modules_Core_Services_Module::getById($module)) != null) : ?>
								<?php echo $this->TRANSLATOR->translator('module_status_installed'); ?>
							<?php else : ?>
								<?php echo $this->TRANSLATOR->translator('module_status_uninstalled'); ?>
							<?php endif; ?>
						</td>
						<td class="w_12">
							<?php if ($moduleInstalled != null && $this->ACL->isAllowed('uninstall', 'module', 'core')) : ?>
							<a href="javascript: void(0);" rel="uninstall_<?php echo $module; ?>" class="button red g_a_actions_module">
								<small class="icon cross"></small><span><?php echo $this->TRANSLATOR->translator('module_actions_uninstall'); ?></span>
							</a>
							<?php elseif (null == $moduleInstalled && $this->ACL->isAllowed('install', 'module', 'core')) : ?>
							<a href="javascript: void(0);" rel="install_<?php echo $module; ?>" class="button green g_a_actions_module">
								<small class="icon check"></small><span><?php echo $this->TRANSLATOR->translator('module_actions_install'); ?></span>
							</a>
							<?php endif; ?>
						</td>
					</tr>
					<?php endforeach; ?>
					<?php endif; ?>
				</tbody>
			</table>
			<!--  -->	
		</div>
	</div>
	<div class="body_footer">
		<div class="bottom_left"><div class="bottom_right"></div></div>
	</div>
</div>
<script type="text/javascript">
'Gio.Core.Module'.namespace();
Gio.Core.Module.currenAction = null;
Gio.Core.Module.actions = function(params) {
	/**
	 * Show g_a_ajax_loading element
	 */
	$('#g_a_ajax_loading').show();
	$.ajaxq('core_module_action', {
		type: 'post',
		url: params.url,
		data: params,
		success: function(response) {
			if (response == 'RESULT_OK') {
				var type = 'success';
				$('#gCoreModuleRow_' + params.module_id).addClass('g_a_row_affected');
				switch (params.act) {
					case 'install':
						<?php if (!$this->ACL->isAllowed('uninstall', 'module', 'core')) : ?>
						$(Gio.Core.Module.currenAction).remove();
						break;
						<?php endif; ?>
					case 'uninstall':
						var rel = (params.act == 'install') ? 'uninstall_' + params.module_id : 'install_' + params.module_id;
						var className = (params.act == 'install') ? 'cross' : 'check';
						var color = (params.act == 'install') ? 'red' : 'green';
						var text = (params.act == 'install') ? '<?php echo $this->TRANSLATOR->translator('module_actions_uninstall'); ?>' : '<?php echo $this->TRANSLATOR->translator('module_actions_install'); ?>';
						var textStatus = (params.act == 'install') ? '<?php echo $this->TRANSLATOR->translator('module_status_installed'); ?>' : '<?php echo $this->TRANSLATOR->translator('module_status_uninstalled'); ?>';
						var message = (params.act == 'install') ? '<?php echo $this->TRANSLATOR->translator('module_actions_install_success'); ?>' : '<?php echo $this->TRANSLATOR->translator('module_actions_uninstall_success'); ?>';

						$(Gio.Core.Module.currenAction).attr('rel', rel)
														.removeClass('red')
														.removeClass('green')
														.addClass(color);
						$(Gio.Core.Module.currenAction).children('small:first')
														.removeClass('cross')
														.removeClass('check')
														.addClass(className);
						$(Gio.Core.Module.currenAction).children('span:first').html(text);
						$('#gCoreModuleRowStatus_' + params.module_id).html(textStatus);
						<?php if (!$this->ACL->isAllowed('install', 'module', 'core')) : ?>
						$(Gio.Core.Module.currenAction).remove();
						<?php endif; ?>
						break;
				}
				Gio.Message.success(message);
			} else {
				Gio.Message.error('<?php echo $this->STRING->escape($this->TRANSLATOR->translator('common_error_message')); ?>');
			}
			/**
			 * Hide g_a_ajax_loading element
			 */
			$('#g_a_ajax_loading').hide();
		}
	});
};

$(document).ready(function() {
	$('.g_a_actions_module').each(function() {
		var self = this;
		$(self).bind('click', function() {
			Gio.Core.Module.currenAction = this;
			var stringId = $(this).attr('rel');
			var temp = stringId.split('_');
			var act = temp[0];
			var id = temp[1];
			var url = ''; 
			switch (act) {
				case 'install':
					url = '<?php echo $this->url('core_module_install'); ?>';
					var messsage_confirm = '<?php echo $this->STRING->escape($this->TRANSLATOR->translator('module_actions_install_confirm')); ?>';
					break;
				case 'uninstall':
					url = '<?php echo $this->url('core_module_uninstall'); ?>';
					var messsage_confirm = '<?php echo $this->STRING->escape($this->TRANSLATOR->translator('module_actions_uninstall_confirm')); ?>';
					break;
				default:
					var url = null;
					var messsage_confirm = null;
					break;
			}
			data = { url: url, module_id: id, act: act };
			Gio.Message.yesButton = '<?php echo $this->STRING->escape($this->TRANSLATOR->translator('common_yes_button', 'core')); ?>';
			Gio.Message.noButton = '<?php echo $this->STRING->escape($this->TRANSLATOR->translator('common_no_button', 'core')); ?>';
			Gio.Message.confirm(messsage_confirm, 'Gio.Core.Module.actions', data);
		});
	});
});
</script>